package com.woniuxy.background.service.impl;

import com.github.pagehelper.PageInfo;
import com.woniuxy.background.dao.GoodsMapper;
import com.woniuxy.background.dao.GoodsTypeMapper;
import com.woniuxy.background.pojo.Goods;
import com.woniuxy.background.service.GoodsService;
import com.woniuxy.background.vo.GoodVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsTypeMapper goodsTypeMapper;

    @Override
    public int deleteByPrimaryKey(Integer goodsId) {
        return goodsMapper.deleteByPrimaryKey(goodsId);
    }

    @Override
    public int insert(Goods record) {
        return goodsMapper.insert(record);
    }

    @Override
    public int insertSelective(Goods record) {
        return goodsMapper.insertSelective(record);
    }

    @Override
    public Goods selectByPrimaryKey(Integer goodsId) {
        return goodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public int updateByPrimaryKeySelective(Goods record) {
        return goodsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Goods record) {
        return goodsMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo queryGoodsByPage(Integer currentPage, Integer pageSize, GoodVO vo) {
        List<Goods> goodsList = goodsMapper.queryGoodsByPage(currentPage, pageSize, vo);
        PageInfo info = new PageInfo(goodsList);
        List<Goods> gs = new ArrayList<Goods>();
        for (Goods g : goodsList){
            g.setGoodsTypeName(goodsTypeMapper.selectByPrimaryKey(g.getGoodsTypeId()).getGoodsTypeName());
            gs.add(g);
        }
        info.setList(gs);
        return info;
    }
}
